<template>
    <div>
        <div class="card">
            <el-input clearable style="width: 260px; margin-right: 5px" v-model="data.title" placeholder="请输入标题查询" :suffix="Search"></el-input>
            <el-button type="primary" icon="Search" @click="load"/>
            <el-button @click="reset">重置</el-button>
            <el-button type="success" @click="handleAdd">新增</el-button>
        </div>

        <div class="card">
            <el-table :data="data.tableData" style="width: 100%" @selection-change="handleSelectionChange"
                      :header-cell-style="{ color: '#333', backgroundColor: '#eaf4ff' }">
                <el-table-column type="selection" width="30" />
                <el-table-column label="图片" width="120px">
                    <template #default="scope">
                        <el-image :src="scope.row.picture" :preview-src-list="[scope.row.picture]" :preview-teleported="true"
                                  style="width: 110px; height: 110px;" />
                    </template>
                </el-table-column>
                <el-table-column prop="title" label="标题"  />
                <el-table-column prop="content" label="公告内容"  />
                <el-table-column label="操作" >
                    <template #default="scope">
                        <el-button type="primary" icon="Edit" @click="handleEdit(scope.row)"></el-button>
                        <el-button type="danger" icon="Delete" circle @click="del(scope.row.id)"></el-button>
                    </template>
                </el-table-column>
            </el-table>

        </div>

        <div class="card">
            <el-pagination
                v-model:current-page="data.pageNum"
                v-model:page-size="data.pageSize"
                layout="total, sizes,prev, pager, jumper"
                :total="data.total"
                @current-change="load"
                @size-change="load"
            />
        </div>

    </div>
    <!-- 弹窗dialog -->
    <el-dialog >

    </el-dialog>

</template>

<script setup>

import {reactive} from "vue";
import {Search} from "@element-plus/icons-vue";
import request from "@/utils/request.js";
import {ElMessage, ElMessageBox} from "element-plus";

const data = reactive({
    user: JSON.parse(localStorage.getItem('userInfo')),
    title:null,
    content:null,
    picture:null,
    pageNum:1,
    pageSize:5,
    total:0,
    tableData:[],
    formVisible:false,
    form:{},
    rules: {
        title: [
            { required: true, message: "请填写标题", trigger: "blur" }
        ],
        content: [
            { required: true, message: "请填写公告内容", trigger: "blur" }
        ]
    },
    rows:[]
})
const load = () => {
    request.get('/notice/selectPage',{
            params:{
                pageNum:data.pageNum,
                pageSize:5,
                title:data.title,
            }
    }).then(res =>{
        if(res.code == '200') {
            data.tableData = res.data.list
            data.total = res.data.total
        }else {
            ElMessage.error(res.msg)
        }
    })
}
load()
const reset = () => {
    data.title = null
    load()
}
const handleAdd = () => {
    data.formVisible = true
    data.form = {}
}
const handleSelectionChange = (rows) =>{  //实际选择的行组成的数组
    console.log(rows)
    data.rows = rows
}
const handleEdit = (row) => {
    data.form = JSON.parse(JSON.stringify(row))  //深度拷贝数据
    data.formVisible = true
}

const del = (id) => {
    ElMessageBox.confirm('删除后无法恢复，是否确认删除？', '删除确认',{type: 'waring'}).then(res =>{
        request.delete('/user/delete/' + id).then(res => {
            if (res.code == '200') {
                ElMessage.success('删除成功')
                userpage()
            } else {
                ElMessage.error(res.msg)
            }
        })
    }).catch(err => {'已取消删除'})
}
</script>
